/*
 * Copyright (C) 2013 Tomi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.ui.components;

import rpg.gfx.Bitmap;
import rpg.util.ImageUtil;

/**
 *
 * @author Tomi
 */
public class ComponentRadioButton extends Component implements IClickableComponent, ISelectOne {

    private boolean checked = false;
    
    private Bitmap background = ImageUtil.loadBitmap("/res/ui/radio.png");
    
    private Bitmap tick = ImageUtil.loadBitmap("/res/ui/radio_selection.png");
    private ComponentGroup cg;

    public ComponentRadioButton(Integer x, Integer y, ComponentGroup chold) {
        super(30, 30, x, y);
        this.cg = chold;
       
        chold.addComponent(this);
    }

    public ComponentRadioButton(Integer x, Integer y, ComponentGroup chold, boolean checked) {
        this(x, y, chold);
        this.checked = checked;
    }

    @Override
    public void redraw() {
        clear();
        if (state == ComponentState.HOVER) {
            draw(background.colorizeBitmap(0xff0000), 0, 0);
        } else if (state == ComponentState.SELECTED) {
            draw(background.colorizeBitmap(0xffff00), 0, 0);
        } else {
            draw(background, 0, 0);
        }
        
        
        if (checked) {
            draw(tick, 0, 0);
        }
        if(parent != null){
            ((Component)parent).redraw();
        }
    }

    @Override
    public boolean onPress(int x, int y) {
        return false;
    }

    @Override
    public boolean onRelease(int x, int y) {
        if (isMouseOver(x, y) && isInside(x, y)) {
            cg.setSelectedComponent(this, true);
        }
        return false;
    }

    @Override
    public boolean isChecked() {
        return checked;
    }

    public boolean isInside(int mouseX, int mouseY) {
        final int cX = x + width / 2;
        final int cY = y + height / 2;
        final int radiusPower = (width / 2) * (width / 2);
        double distance = (mouseX - cX) * (mouseX - cX) + (mouseY - cY) * (mouseY - cY);
        return distance < radiusPower;
    }

    @Override
    public void setChecked(boolean b) {
        checked = b;
    }
}
